<?php

class Core_Image {
	private $_width = 0;
	private $_height = 0;
	private $_img = null;
	private $_quality = 0; // Default in php
	private $_path = "";
	
	public function __construct($img_path, $quality = 75) {
		$this->_quality = $quality;
		if (! file_exists ( $img_path ))
			throw new Exception ( "File, doesn`t exist!" );
		list ( $this->_width, $this->_height ) = getimagesize ( $img_path );
		$this->_img = imagecreatefromjpeg ( $img_path );
		$this->_path = $img_path;
	}
	public function setQuality($new_quality) {
		$this->_quality = $new_quality;
	}
	public function resize($max_width, $max_height) {
		$xscale = ( float ) ($this->_width / $max_width);
		$yscale = ( float ) ($this->_height / $max_height);
		
		$mult = 1 / (($yscale > $xscale) ? $yscale : $xscale);
		
		$new_width = round ( $this->_width * $mult );
		$new_height = round ( $this->_height * $mult );
		
		$tmp_img = imagecreatetruecolor ( $new_width, $new_height );
		imagecopyresampled ( $tmp_img, $this->_img, 0, 0, 0, 0, $new_width, $new_height, $this->_width, $this->_height );
		$this->_img = $tmp_img;
		$this->_width = $new_width;
		$this->_height = $new_height;
	
	}
	public function save($new_path) {
		imagejpeg ( $this->_img, $new_path, $this->_quality );
		$this->_path = $new_path;
	}
}